/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.wx.web.qr;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.wx.entity.qr.WxQr;
import com.server.modules.wx.service.qr.WxQrService;

/**
 * 微信二维码Controller
 * @author 王磊
 * @version 2017-06-21
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/qr/wxQr")
public class WxQrController extends BaseController {

	@Autowired
	private WxQrService wxQrService;


	@ModelAttribute
	public WxQr get(@RequestParam(required=false) String id) {
		WxQr entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wxQrService.get(id);
		}
		if (entity == null){
			entity = new WxQr();
		}
		return entity;
	}
	
	@RequiresPermissions("wx:qr:wxQr:view")
	@RequestMapping(value = {"list", ""})
	public String list(WxQr wxQr, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WxQr> page = wxQrService.findPage(new Page<WxQr>(request, response), wxQr); 
		model.addAttribute("page", page);
		model.addAttribute("wxQr", wxQr);
		return "modules/wx/qr/wxQrList";
	}

	@RequiresPermissions("wx:qr:wxQr:view")
	@RequestMapping(value = "form")
	public String form(WxQr wxQr, Model model) {
		model.addAttribute("wxQr", wxQr);
		return "modules/wx/qr/wxQrForm";
	}

	@RequiresPermissions("wx:qr:wxQr:edit")
	@RequestMapping(value = "save")
	public String save(WxQr wxQr, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wxQr)){
			return form(wxQr, model);
		}
		addMessage(redirectAttributes, "保存微信二维码成功");
		return "redirect:"+Global.getAdminPath()+"/wx/qr/wxQr/?repage";
	}
	
	@RequiresPermissions("wx:qr:wxQr:edit")
	@RequestMapping(value = "delete")
	public String delete(WxQr wxQr, RedirectAttributes redirectAttributes) {
		wxQrService.delete(wxQr);
		addMessage(redirectAttributes, "删除微信二维码成功");
		return "redirect:"+Global.getAdminPath()+"/wx/qr/wxQr/?repage";
	}

}